package com.example.dblibrary.app.dbManager;


import com.example.dblibrary.database.AppDatabase;
import com.example.dblibrary.database.daos.SingerIndexConstant;
import com.example.dblibrary.database.entities.Singer;
import com.example.dblibrary.database.entities.Song;

import java.util.List;

import io.reactivex.Flowable;

public class DbModel implements DbContract.BaseHomeModel {

    private AppDatabase mAppDatabase;

    public DbModel(AppDatabase mAppDatabase) {
        this.mAppDatabase = mAppDatabase;
    }

    @Override
    public Flowable<List<Singer>> loadSingerByOffset(int start, int offset) {
        return this.mAppDatabase.singerDao().loadSingersByPageOffset(start, offset);
    }

    @Override
    public Flowable<List<Singer>> loadSingerByDim(int start, int offset, int type, String typeContent, int area, String areaContent, int firstSpell, String firstSpellContent, int handleSpell, String handleSpellContent) {

        if (type == SingerIndexConstant.SINGS_DEAULT && area == SingerIndexConstant.SINGS_DEAULT && firstSpell == SingerIndexConstant.SINGS_DEAULT && handleSpell == SingerIndexConstant.SINGS_DEAULT) {
            return this.loadSingerByOffset(start, offset);
        } else {
            if (area == SingerIndexConstant.SINGER_AREA && type == SingerIndexConstant.SINGER_TYPE) {

                if (firstSpell == SingerIndexConstant.SINGER_FIRST_SPELL) {
                    //简写
                    return this.mAppDatabase.singerDao().loadSingersInAreaSpByPageOffset(start, offset, typeContent, areaContent, firstSpellContent);
                } else if (handleSpell == SingerIndexConstant.SINGER_NAME) {
                    //拼音
                    return this.mAppDatabase.singerDao().loadSingersInAreaHdByPageOffset(start, offset, typeContent, areaContent, handleSpellContent);
                } else {
                    return this.mAppDatabase.singerDao().loadSingersInAreaByPageOffset(start, offset, typeContent, areaContent);
                }

            } else if (area == SingerIndexConstant.SINGER_AREA || type == SingerIndexConstant.SINGER_TYPE) {
                if (area == SingerIndexConstant.SINGER_AREA) {
                    if (firstSpell == SingerIndexConstant.SINGER_FIRST_SPELL) {
                        //简写
                        return this.mAppDatabase.singerDao().loadSingersInAreaSpByPageOffset(start, offset, "", areaContent, firstSpellContent);
                    } else if (handleSpell == SingerIndexConstant.SINGER_NAME) {
                        //拼音
                        return this.mAppDatabase.singerDao().loadSingersInAreaHdByPageOffset(start, offset, "", areaContent, handleSpellContent);
                    } else {
                        return this.loadSingerByOffset(start, offset);
                    }
                } else {
                    if (firstSpell == SingerIndexConstant.SINGER_FIRST_SPELL) {
                        //简写
                        return this.mAppDatabase.singerDao().loadSingersInAreaSpByPageOffset(start, offset, typeContent, "", firstSpellContent);
                    } else if (handleSpell == SingerIndexConstant.SINGER_NAME) {
                        //拼音
                        return this.mAppDatabase.singerDao().loadSingersInAreaHdByPageOffset(start, offset, typeContent, "", handleSpellContent);
                    } else {
                        return this.loadSingerByOffset(start, offset);
                    }
                }
            } else {
                if (firstSpell == SingerIndexConstant.SINGER_FIRST_SPELL) {
                    //简写
                    return this.mAppDatabase.singerDao().loadSingersInSpByPageOffset(start, offset, firstSpellContent);
                } else if (handleSpell == SingerIndexConstant.SINGER_NAME) {
                    //拼音
                    return this.mAppDatabase.singerDao().loadSingersInHdByPageOffset(start, offset, handleSpellContent);
                } else {
                    return this.loadSingerByOffset(start, offset);
                }
            }
        }
    }

    @Override
    public Flowable<List<Song>> loadSongByDim(int start, int offset, int category, int categoryContent, int firstSpell, String firstSpellContent, int handleSpell, String handleSpellContent, int state) {

        if (category == SingerIndexConstant.SINGS_TYPE) {
            if (category > 0) {
                //音乐类型
                if (firstSpell == SingerIndexConstant.SINGER_FIRST_SPELL) {
                    //简写
                    return this.mAppDatabase.songDao().loadSongsInCgSpByPageOffset(start, offset, categoryContent, state, firstSpellContent);
                } else if (handleSpell == SingerIndexConstant.SINGER_NAME) {
                    //拼音
                    return this.mAppDatabase.songDao().loadSongsInCgHdByPageOffset(start, offset, categoryContent, state, handleSpellContent);
                } else {

                    return this.mAppDatabase.songDao().loadSongsByPageOffset(start, offset, categoryContent, state);
                }
            } else {
                if (firstSpell == SingerIndexConstant.SINGER_FIRST_SPELL) {
                    //简写
                    return this.mAppDatabase.songDao().loadSongsInCgSpByPageOffset2(start, offset, state, firstSpellContent);
                } else if (handleSpell == SingerIndexConstant.SINGER_NAME) {
                    //拼音
                    return this.mAppDatabase.songDao().loadSongsInCgHdByPageOffset2(start, offset,  state, handleSpellContent);
                } else {

                    return this.mAppDatabase.songDao().loadSongsByPageOffset2(start, offset,  state);
                }
            }
        } else {
            if (firstSpell == SingerIndexConstant.SINGER_FIRST_SPELL) {
                //简写
                return this.mAppDatabase.songDao().loadSongsInCgSpByPageOffset2(start, offset,  state, firstSpellContent);
            } else if (handleSpell == SingerIndexConstant.SINGER_NAME) {
                //拼音
                return this.mAppDatabase.songDao().loadSongsInCgHdByPageOffset2(start, offset,  state, handleSpellContent);
            } else {

                return this.mAppDatabase.songDao().loadSongsByPageOffset2(start, offset, state);
            }
        }
    }

    @Override
    public Flowable<List<Song>> loadSongsByBatch(int start, int offset, int state, List<Integer> nums) {
        return this.loadSongsByBatch(start, offset, state, nums);
    }
}
